package com.vn.gofriends.fragment;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.vn.gofriends.R;
import com.vn.gofriends.activity.CreateMyTripActivity;
import com.vn.gofriends.adapter.TotalMyTripAdapter;
import com.vn.gofriends.item.Trip;

@SuppressLint("NewApi")
public class MyTripFragment extends Fragment {
	public static final int REQUEST_CODE = 100;
	// Declare Variables

	Button btnCreate;
	ListView lvTotalTrip;
	TotalMyTripAdapter totalMyTripAdapter;
	ArrayList<Trip> trips;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = null;
		view = inflater.inflate(R.layout.layout_total_mytrip, container, false);
		btnCreate = (Button) view.findViewById(R.id.btnCreateTotalTrip);
		lvTotalTrip = (ListView) view.findViewById(R.id.lvTotalMyTrip);
		
		//get data
		trips = new ArrayList<Trip>();
		//end 
		totalMyTripAdapter = new TotalMyTripAdapter(getActivity(),trips);
		lvTotalTrip.setAdapter(totalMyTripAdapter);
		updateTrip();
		btnCreate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				startActivityForResult(new Intent(getActivity().getApplicationContext(),CreateMyTripActivity.class) , REQUEST_CODE);
			}
		});
		lvTotalTrip.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Fragment fragment = new ItemMyTripFragment();
				
				Bundle bundle = new Bundle();
				bundle.putSerializable("KeyTrip", trips.get(arg2));
				fragment.setArguments(bundle);
				FragmentManager fragmentManager = getFragmentManager();
				fragmentManager.beginTransaction()
						.replace(R.id.frame_container, fragment).commit();
			}
		});
		return view;
	}
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == REQUEST_CODE){
			if (resultCode == Activity.RESULT_OK) {
				updateTrip();
			}
		}
	}
	public void updateTrip() {
		final ParseQuery<ParseObject> query = ParseQuery.getQuery("Trip");
		query.whereEqualTo("idOwner", ParseUser.getCurrentUser());
		
		query.findInBackground(new FindCallback<ParseObject>() {

			@Override
			public void done(List<ParseObject> tripList, ParseException e) {
				if (e == null) {
					trips.clear();
					for (ParseObject trip : tripList) {
						Trip iTrip = new Trip();
						iTrip.setId(trip.getObjectId());
						iTrip.setTitle(trip.getString("title"));
						iTrip.setPubic(trip.getBoolean("isPublic"));
						iTrip.setDateInit(trip.getDate("dateInit"));
						iTrip.setDateEnd(trip.getDate("dateEnd"));
						iTrip.setIdOwner(trip.getParseObject("idOwner"));
						iTrip.setUserameOwner(ParseUser.getCurrentUser().getUsername());
						iTrip.setDescription(trip.getString("description"));
						
						trips.add(iTrip);
						Log.d("Vien", "Description: "+ iTrip.getDescription() + " User: "+ iTrip.getIdOwner().getObjectId());
					}
					 totalMyTripAdapter.notifyDataSetChanged();
				} else {
					Log.d("Post retrieval", "Error: " + e.getMessage());
				}

			}
		});}
}
